package cn.com.mypm.common.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.NetworkInterface;
import java.util.Enumeration;

public class MenuInit {

	public static String getInitMenuSql(String pathAndFile, String compId)
			throws Exception {

		InputStreamReader fr = new InputStreamReader(new FileInputStream(
				pathAndFile), "utf-8");
		BufferedReader br = new BufferedReader(fr);
		String line = br.readLine();
		int i = 1;
		int level1 = 0;
		int level2 = 0;
		int level3 = 0;
		int level4 = 0;
		int level5 = 0;
		int level6 = 0;
		String url = "";
		String secuUrl = "";
		StringBuffer sb = new StringBuffer("\n");
		while (line != null) {
			url = "";
			secuUrl = "";
			String sql = null;
			String myLine = line.trim();
			String page = "0";
			if (myLine.split(",").length > 2) {
				url = myLine.split(",")[2];
			}
			if (myLine.split(",").length > 3) {
				secuUrl = myLine.split(",")[3];
			}
			if (myLine.split(",").length > 4) {
				page = myLine.split(",")[4];
			}

			if (!myLine.equals("")) {
				if (i % 10 == 0) {
					i = i + 10;
				}
				sql = "insert into  T_FUNCTION(FUNCTIONID,FUNCTIONNAME,SEQ,PARENTID,LEVELNUM,ISLEAF,URL,SECURITY_URL,PAGE) values('"
						+ String.valueOf(i)
						+ "','"
						+ myLine.substring(1, myLine.length()).split(",")[0]
						+ "'," + i + ",'";
				if (myLine.startsWith("1")) {
					level1 = i;
					sql += String.valueOf(-1)
							+ "',1,"
							+ myLine.substring(1, myLine.length()).split(",")[1]
							+ ",'" + url + "','" + secuUrl + "','" + page
							+ "')";
				}
				if (myLine.startsWith("2")) {
					level2 = i;
					sql += String.valueOf(level1)
							+ "',2,"
							+ myLine.substring(1, myLine.length()).split(",")[1]
							+ ",'" + url + "','" + secuUrl + "','" + page
							+ "')";
				}
				if (myLine.startsWith("3")) {
					level3 = i;
					sql += String.valueOf(level2)
							+ "',3,"
							+ myLine.substring(1, myLine.length()).split(",")[1]
							+ ",'" + url + "','" + secuUrl + "','" + page
							+ "')";
				}
				if (myLine.startsWith("4")) {
					level4 = i;
					sql += String.valueOf(level3)
							+ "',4,"
							+ myLine.substring(1, myLine.length()).split(",")[1]
							+ ",'" + url + "','" + secuUrl + "','" + page
							+ "')";
				}
				if (myLine.startsWith("5")) {
					level5 = i;
					sql += String.valueOf(level4)
							+ "',4,"
							+ myLine.substring(1, myLine.length()).split(",")[1]
							+ ",'" + url + "','" + secuUrl + "','" + page
							+ "')";
				}
				if (myLine.startsWith("6")) {
					level6 = i;
					sql += String.valueOf(level5)
							+ "',4,"
							+ myLine.substring(1, myLine.length()).split(",")[1]
							+ ",'" + url + "','" + secuUrl + "','" + page
							+ "')";
				}
				if (myLine.startsWith("7")) {
					sql += String.valueOf(level6)
							+ "',4,"
							+ myLine.substring(1, myLine.length()).split(",")[1]
							+ ",'" + url + "','" + secuUrl + "','" + page
							+ "')";
				}
				String sql2 = "insert into T_ROLE_FUNCTION_REAL(ROLEID,FUNCTIONID)  values('"
						+ compId + "','" + i + "');";
				sb.append(sql).append(";\n");
				sb.append(sql2 + "\n");
				i++;
			}
			line = br.readLine();
		}
		return sb.toString();
	}

	


}
